package com.caiwangdai.app.extension

import android.animation.ObjectAnimator
import android.os.CountDownTimer
import android.support.v4.content.ContextCompat
import android.view.ContextThemeWrapper
import android.view.ViewGroup
import android.view.animation.LinearInterpolator
import android.widget.ImageView
import com.afollestad.materialdialogs.MaterialDialog
import com.caiwangdai.app.R
import com.caiwangdai.app.bean.LoanProductB
import com.caiwangdai.app.bean.RepayOrderDetailB
import kotlinx.android.synthetic.main.dialog_comprehensive_fee.view.*
import kotlinx.android.synthetic.main.dialog_order_fee.view.*
import kotlinx.android.synthetic.main.dialog_verify_waiting.view.*
import java.io.File

/**
 * Author:你需要一台永动机
 * Date:2017/11/14 11:51
 * Description:
 */
fun ContextThemeWrapper.showTipDialog(message: String) {
    MaterialDialog.Builder(this)
            .cancelable(true)
            .backgroundColor(ContextCompat.getColor(this, R.color.white))
            .title("提示")
            .content("" + message)
            .show()
}


fun ContextThemeWrapper.showTipDiaglogWithBtn(message: String, dialogClick: OnDialogCenterClick) {
    MaterialDialog.Builder(this)
            .cancelable(true)
            .backgroundColor(ContextCompat.getColor(this, R.color.white))
            .title("提示")
            .content("" + message)
            .positiveText("确定")
            .onPositive { dialog, which -> dialogClick.onPositiveClick() }
            .negativeText("取消")
            .onNegative { dialog, which -> dialogClick.onNegativeClick() }
            .show()
}


//缺失权限的提示
fun ContextThemeWrapper.showMissingPermissionDialog(permissionTips: String) {
    MaterialDialog.Builder(this)
            .cancelable(false)
            .backgroundColor(ContextCompat.getColor(this, R.color.white))
            .title("提示")
            .content("" + permissionTips)
            .positiveText("去开启")
            .negativeText("知道了")
            .onPositive { dialog, which -> this.openAppInfoPage() }
            .show()
}
//显示更新dialog
fun ContextThemeWrapper.showUpdateDialog(file: File) {
    MaterialDialog.Builder(this)
            .cancelable(true)
            .backgroundColor(ContextCompat.getColor(this, R.color.white))
            .title("软件更新")
            .content("新版本已下载完成,请立即安装更新！")
            .positiveText("立即安装")
            .onPositive { dialog, which -> openApkByFilePath(file) }
            .show()
}


interface OnDialogCenterClick {
    fun onNegativeClick() {

    }

    fun onPositiveClick() {

    }
}


//综合费用
fun ContextThemeWrapper.showComprehensiveFeeDialog(loanProduct: LoanProductB) {
    val feeList = loanProduct.fee_list
    //dialog
    val dialog: MaterialDialog = MaterialDialog.Builder(this)
            .cancelable(true)
            .backgroundColor(ContextCompat.getColor(this, R.color.transparent))
            .customView(R.layout.dialog_comprehensive_fee, false)
            .show()
    //views
    val contentView = dialog.customView as ViewGroup
    //close
    contentView.iv_close.setOnClickListenerPro {
        dialog.dismiss()
    }
    val feeObj = loanProduct.fee_list
    //管理费用
    contentView.tv_manage_fee.text = "￥${feeObj?.manager_fee}"
    //审核服务费
    contentView.tv_auth_fee.text = "￥${feeObj?.auth_fee}"
    //征信服务费
    contentView.tv_xin_fee.text = "￥${feeObj?.xin_fee}"
    //综合费用
    contentView.tv_total_fee.text = "￥${feeObj?.all_fee}"
}


//订单 费用明细
fun ContextThemeWrapper.showOrderFeeDialog(feeDetail: RepayOrderDetailB) {
    //dialog
    val dialog: MaterialDialog = MaterialDialog.Builder(this)
            .cancelable(true)
            .backgroundColor(ContextCompat.getColor(this, R.color.transparent))
            .customView(R.layout.dialog_order_fee, false)
            .show()
    //views
    val contentView = dialog.customView as ViewGroup
    val repayInfo = feeDetail.info?.repay_info
    with(contentView) {
        //close
        iv_order_fee_close.setOnClickListenerPro {
            dialog.dismiss()
        }
        //capital
        tv_capital.text = "￥${repayInfo?.money}"
        //interest
        tv_interest.text = "￥${repayInfo?.interest_money}"
        //total
        tv_total.text = "￥${repayInfo?.sum_money}"
    }
}


//验证等待
fun ContextThemeWrapper.showVerifyWaitingDialog(): MaterialDialog {
    //dialog
    val verifyDialog = MaterialDialog.Builder(this)
            .cancelable(false)
            .backgroundColor(ContextCompat.getColor(this, R.color.transparent))
            .customView(R.layout.dialog_verify_waiting, false)
            .show()
    //anim
    val customView = verifyDialog.getCustomView() as ViewGroup

    val waitIconList = mutableListOf<ImageView>()

    waitIconList.add(customView.iv_wait_1 as ImageView)
    waitIconList.add(customView.iv_wait_2 as ImageView)
    waitIconList.add(customView.iv_wait_3 as ImageView)

    val rotation = waitIconList[0].rotation

    val valOne = ObjectAnimator.ofFloat(waitIconList[0], "rotation", 0.0f, 360f)
    valOne.interpolator = LinearInterpolator()
    valOne.duration = 700
    valOne.repeatCount = -1
    valOne.start()


    val valSec = ObjectAnimator.ofFloat(waitIconList[1], "rotation", 0.0f, 360f)
    valSec.interpolator = LinearInterpolator()
    valSec.duration = 700
    valSec.repeatCount = -1
    valSec.start()

    val valThi = ObjectAnimator.ofFloat(waitIconList[2], "rotation", 0.0f, 360f)
    valThi.interpolator = LinearInterpolator()
    valThi.duration = 700
    valThi.repeatCount = -1
    valThi.start()

    val cd = object : CountDownTimer(6000, 1000) {
        override fun onFinish() {
//                verifyDialog.dismiss()
        }

        override fun onTick(l: Long) {
            when ("${l / 1000}") {
                "4" -> {
                    valOne.cancel()
                    waitIconList[0].rotation = rotation
                    waitIconList[0].setImageResource(R.drawable.ic_verify_ok)
                }
                "2" -> {
                    valSec.cancel()
                    waitIconList[1].rotation = rotation
                    waitIconList[1].setImageResource(R.drawable.ic_verify_ok)

                }
            }
        }
    }
    cd.start()
    return verifyDialog
}
